package com.example.mall

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.GridView
import android.widget.ImageView
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.example.mall.adapter.GoodsAdapter
import com.example.mall.dao.UserDao
import com.example.mall.entity.CartDTO
import com.example.mall.entity.GoodsInfo
import com.example.mall.entity.User
import com.example.mall.network.CartService
import com.example.mall.network.GoodsService
import com.example.mall.network.ServiceCreator
import com.example.mall.network.UserService
import com.example.mall.util.ToastUtil
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class MallActivity : AppCompatActivity(), View.OnClickListener, GoodsAdapter.AddCartListener {

    var tv_count : TextView? = null
    var gv_mall : GridView? = null
    val goodsService = ServiceCreator.create<GoodsService>()
    val cartService = ServiceCreator.create<CartService>()
    var goodsList : List<GoodsInfo>? = null
    var app : MyApplication?= null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_mall)
        app = application as MyApplication

        val tv_title = findViewById<TextView>(R.id.tv_title)
        tv_title.setText("手机商城")

        tv_count = findViewById(R.id.tv_count)
        gv_mall = findViewById(R.id.gv_mall)
        findViewById<ImageView>(R.id.iv_back).setOnClickListener(this)
        findViewById<ImageView>(R.id.iv_cart).setOnClickListener(this)

        getGoodsList()

        //showGoods()
    }

    private fun getGoodsList() {
        goodsService.getGoodsList().enqueue(object :
            Callback<List<GoodsInfo>> {
            override fun onResponse(call: Call<List<GoodsInfo>>,
                                    response: Response<List<GoodsInfo>>
            ) {
                goodsList = response.body()
                showGoods()
            }

            override fun onFailure(p0: Call<List<GoodsInfo>>, p1: Throwable) {
                ToastUtil.show(this@MallActivity,"网络异常")
            }
        })
    }

    private fun showGoods() {
        val adapter = GoodsAdapter(this, goodsList!!,this)
        gv_mall?.adapter = adapter
    }

    override fun onResume() {
        super.onResume()
        showCartInfoTotal()

    }

    private fun showCartInfoTotal() {

        cartService.getCartCount(app?.userId!!).enqueue(object :
            Callback<Int> {
            override fun onResponse(call: Call<Int>,
                                    response: Response<Int>
            ) {
                app?.cartCount = response.body()
                tv_count?.setText(app?.cartCount.toString())
            }

            override fun onFailure(p0: Call<Int>, p1: Throwable) {
                ToastUtil.show(this@MallActivity,"网络异常")
            }
        })


    }

    override fun onClick(v: View?) {
        when(v?.id){
            R.id.iv_back -> {
                finish()
            }
            R.id.iv_cart -> {
                val intent = Intent(this,CartActivity::class.java)
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
                startActivity(intent)
            }
        }
    }

    override fun addToCart(goodsId: Int) {
        cartService.addToCart(CartDTO(app?.userId!!,goodsId)).enqueue(object :
            Callback<Int> {
            override fun onResponse(call: Call<Int>,
                                    response: Response<Int>
            ) {
                app?.cartCount = response.body()
                tv_count?.setText(app?.cartCount.toString())
            }

            override fun onFailure(p0: Call<Int>, p1: Throwable) {
                ToastUtil.show(this@MallActivity,"网络异常")
            }
        })
    }
}